package com.cn.qgx428.lazydiary.ui.activity

import android.content.Context
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.cn.qgx428.lazydiary.Presenter.DiaryPresenter
import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.Utils.MConst
import com.cn.qgx428.lazydiary.Utils.SPUtil
import com.cn.qgx428.lazydiary.adapter.RAdapter
import com.cn.qgx428.lazydiary.adapter.RViewHolder
import com.cn.qgx428.lazydiary.dialog.CustomAlertDialog
import com.cn.qgx428.lazydiary.local.LAMessege
import com.cn.qgx428.lazydiary.local.LAction
import com.cn.qgx428.lazydiary.local.LActionWacher
import com.cn.qgx428.lazydiary.local.LManager
import com.cn.qgx428.lazydiary.network.bean.Diary
import com.cn.qgx428.lazydiary.ui.activity.subActivity.DiaryEditActivity
import kotlinx.android.synthetic.main.activity_diary.*
import org.jetbrains.anko.startActivity

class DiaryActivity : BaseActivity(), LActionWacher {

    lateinit var dialog: CustomAlertDialog
    val adapter = DiaryAdapter(this, R.layout.adapter_diary_re)

    override fun initUI() {
        hindActionBar()
        setStatuBarColor(Color.BLACK)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_diary)
        if (SPUtil.checkUserLogin()){
            initView()
            getDiaryData()
            LManager.newInstance().register(this,LAction.ACTION_CREATE_DIARY)
            LManager.newInstance().register(this,LAction.ACTION_UPDATE_DIARY)
        } else{
            prumpAlert()
            LManager.newInstance().register(this,LAction.ACTION_LOGIN)
        }
    }

    private fun getDiaryData() {
        DiaryPresenter.getDiaries(SPUtil.currntUserId().toInt(),{
            if (it.diaries != null){
                adapter.setDatas(it.diaries as MutableList<Diary>)
            }
        })
    }

    private fun initView() {
        diary_bar.setOnClickListener{ finish() }
        diary_add.setOnClickListener{ startActivity<DiaryEditActivity>(MConst.DIARY_TYPE to MConst.DIARY_CREATE) }
        diary_re.layoutManager = LinearLayoutManager(this)
        diary_re.adapter = adapter
    }

    private fun prumpAlert() {
        dialog = getCustomAlertDialog()
        dialog.setMessage("请先登陆才能使用哦～")
        dialog.setNeg {
            dialog.dismiss()
            finish()
        }
        dialog.setPosText("登陆")
        dialog.setPos {
            startActivity<LoginActivity>()
            overridePendingTransition(R.anim._loginactivity_in, R.anim._activity_conteract)
        }
        dialog.show()
    }

    override fun onTrigger(action: String, msg: LAMessege) {
        when(action){
            LAction.ACTION_LOGIN ->{
                dialog.dismiss()
                initView()
                getDiaryData()
            }
            LAction.ACTION_UPDATE_DIARY, LAction.ACTION_CREATE_DIARY->{
                getDiaryData()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        LManager.newInstance().unRegister(this)
    }

    class DiaryAdapter: RAdapter<Diary>{
        constructor(context: Context, layoutId: Int): super(context, layoutId)

        override fun conver(holder: RViewHolder, data: Diary) {
            val title = holder.getView<TextView>(R.id.adapter_diary_title)
            val time = holder.getView<TextView>(R.id.adapter_diary_time)
            val mainLayout = holder.getView<LinearLayout>(R.id.adapter_diary_mainLayout)

            time?.text = data.createtime
            title?.text = data.title
            mainLayout?.setOnClickListener {
                mContext.startActivity<DiaryEditActivity>(
                        MConst.DIARY_TYPE to MConst.DIARY_EDIT,
                        MConst.DIARY_BUNDLE to data
                )
            }
        }
    }
}
